package com.zcf.gameController.gameUtils;

import com.zcf.gameController.F_Websocket_s;
import com.zcf.pojo.Msg;
import com.zcf.pojo.RoomsParam;
import com.zcf.pojo.UserBean;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
/**
 * @author ： jj
 * @date ：2019/11/20 9:29
 * @description :
 * @path : SgCountdownTimer.java.SgCountdownTimer
 */
public class SgCountdownTimer extends Thread{

	private  Integer timer;

	private RoomsParam myRoom;

	private F_Websocket_s ws;
	private Map<String, Object> data = new HashMap<>();
	public SgCountdownTimer(Integer timer, RoomsParam myRoom, F_Websocket_s ws){
		this.timer = timer;
		this.ws = ws;
		this.myRoom = myRoom;
	}

	@Override
	public void run() {
		while (timer >0 ) {
			if ( Thread.currentThread().isInterrupted() ) {
				System.out.println("线程终止");
				break;
			}
			try {
				Thread.sleep(1000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			System.out.println("准备倒计时"+timer);
			timer --;
			myRoom.setTimer(timer);
			if(timer <= 0){
				if (!myRoom.getSitting().equals(1)){
					List<UserBean> userBeans =myRoom.getUsers().values().stream().filter(userBean->userBean.getIsReady().equals(1)).collect(Collectors.toList());
					Integer robsTime =myRoom.getRoomType().get("robsTime") ;
					myRoom.setIsStart(2);
					data.put("robsTime",robsTime);
					data.put("isStart" , myRoom.getIsStart());
					data.put("users" ,userBeans);
					data.put("positions" ,myRoom.getPostions());
					Msg msgs = new Msg(200,"开始抢庄", data,"robsGame");
					System.out.println("倒计时结束！！！！");
					myRoom.setTimer(robsTime);
					try {
						ws.broadcast(msgs, myRoom);
					} catch (Exception e) {
						e.printStackTrace();
					}
					myRoom.setTimer(30);
				}else {
					myRoom.setTimer(1);
				}
				SgRobsTimer rt = new SgRobsTimer(myRoom.getTimer(),myRoom,ws);
				rt.start();
			}
		}
	}



}
